from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from home_design.models import InspirationComment
from home_design.serializers import InspirationCommentCrateSerializer, InspirationCommentSerializer


class InspirationCommentCreateView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]

    def post(self, request):
        """处理POST请求，创建灵感作品的评论"""
        serializer = InspirationCommentCrateSerializer(data=request.data, context={'request': request})
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class InspirationCommentDetailView(APIView):

    def get(self, request, inspiration_pk):
        """处理GET请求，获取灵感作品的评论"""
        inspiration_comments = InspirationComment.objects.filter(inspiration_id=inspiration_pk)
        serializer = InspirationCommentSerializer(inspiration_comments, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
